<template>
	<view class="app">
		
		<!-- <web-view src="http://pay.famitech.cn/cashier/index.html#/hub/23953425ccc9fc0c20046df2653da8245c42e417aa8af6c339b012759568c604"></web-view> -->
		<view class="price-box">
			<text>支付金额</text>
			<text class="price">{{orderInfo.payAmount}}</text>
		</view>

		<view class="pay-type-list">

			<view class="type-item b-b" @click="changePayType(2)">
				<text class="icon yticon icon-weixinzhifu"></text>
				<view class="con">
					<text class="tit">微信支付</text>
					<text>推荐使用微信支付</text>
				</view>
				<label class="radio">
					<radio value="" color="#fa436a" :checked='payType == 2' />
					</radio>
				</label>
			</view>
			<!-- <view class="type-item b-b" @click="changePayType(1)">
				<text class="icon yticon icon-alipay"></text>
				<view class="con">
					<text class="tit">支付宝支付</text>
				</view>
				<label class="radio">
					<radio value="" color="#fa436a" :checked='payType == 1' />
					</radio>
				</label>
			</view> -->
		</view>

		<text class="mix-btn" @click="confirm">确认支付</text>
	</view>
</template>

<script>
	import {
		fetchOrderDetail,
		getOpenid,
		getPayData,
		payOrderSuccess
	} from '@/api/order.js';
	export default {
		data() {
			return {
				orderId: null,
				payType: 2,
				orderInfo: {}
			};
		},
		onLoad(options) {
			this.orderId = options.orderId;
			fetchOrderDetail(this.orderId).then(response => {
				this.orderInfo = response.data;
			});
		},
		methods: {
			//选择支付方式
			changePayType(type) {
				this.payType = type;
			},
			//确认支付
			confirm: async function() {
				uni.showLoading({
					title: '支付请求中',
					mask: true
				});

				let pamount = this.orderInfo.payAmount * 100;
				let _orderId = this.orderId;
				let _payType = this.payType;
				if(_payType === 2){
					console.log("微信支付")
					wx.login({
						success: function (res) {  
							getOpenid({
								jscode: res.code
							}).then(response => {
								getPayData({
									orderId: _orderId,
									payType: _payType,
									mchOrderNo: response.data.mchOrderNo,
									amount: pamount,
									wayCode: "WX_LITE",
									orderTitle: "支付",
									channelExtra: '{"openid": "'+response.data.openid+'"}'
								}).then(resPay => {
									// console.log(resPay);
									wx.requestPayment({
										  timeStamp: resPay.data.timeStamp,
										  nonceStr: resPay.data.nonceStr,
										  package: resPay.data.packageStr,
										  signType: resPay.data.signType,
										  paySign: resPay.data.paySign,
										  success (resg) { 
												console.log("支付成功：", resg)
												payOrderSuccess({
													orderId: _orderId,
													payType: _payType
												}).then(response => {
													console.log(response);
													if(response.code == 200){
														uni.redirectTo({
															url: '/pages/money/paySuccess'
														})
													}else{
														console.log("支付改状态失败")
													}
													uni.hideLoading();
												});
										  },
										  fail (resg) {
											  console.log("支付失败：", resg)
											  uni.hideLoading();
										}
									})
								});
							});
						}  
					});  
				}else if(_payType === 1){
					console.log("支付宝支付")
					var orderInfo = '';  //从服务器获取的订单
					uni.getProvider({
					    service: 'payment',
					    success: function (res) {
					        console.log(res.provider)
					    }
					});

					var orderInfo = 'app_id=2015052600090779&biz_content=%7B%22timeout_express%22%3A%2230m%22%2C%22seller_id%22%3A%22%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22total_amount%22%3A%220.02%22%2C%22subject%22%3A%221%22%2C%22body%22%3A%22%E6%88%91%E6%98%AF%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%22%2C%22out_trade_no%22%3A%22314VYGIAGG7ZOYY%22%7D&charset=utf-8&method=alipay.trade.app.pay&signType=RSA2&timeStamp=2016-08-15%2012%3A12%3A15&version=1.0&paySign=MsbylYkCzlfYLy9PeRwUUIg9nZPeN9SfXPNavUCroGKR5Kqvx0nEnd3eRmKxJuthNUx4ERCXe552EV9PfwexqW%2B1wbKOdYtDIb4%2B7PL3Pc94RZL0zKaWcaY3tSL89%2FuAVUsQuFqEJdhIukuKygrXucvejOUgTCfoUdwTi7z%2BZzQ%3D&nonceStr=&package=';  //从服务器获取的订单
					uni.requestPayment({
						"provider": "alipay",   //固定值为"alipay"
						"orderInfo": orderInfo, //此处为服务器返回的订单信息字符串
						success: function (res) {
							var rawdata = JSON.parse(res.rawdata);
							console.log("支付成功");
						},
						fail: function (err) {
							console.log('支付失败:' + JSON.stringify(err));
						}
					});
				}
				
				
				// uni.redirectTo({
				// 	url: 'http://pay.famitech.cn/cashier/index.html#/hub/23953425ccc9fc0c20046df2653da8245c42e417aa8af6c339b012759568c604'
				// })
				// payOrderSuccess({
				// 	orderId: this.orderId,
				// 	payType: this.payType
				// }).then(response => {
				// 	console.log(response);
				// 	if(response.code == 200){
				// 		console.log("555555555");
				// 		uni.redirectTo({
				// 			url: response.message
				// 		})
				// 	}
				// 	// uni.redirectTo({
				// 	// 	url: '/pages/money/paySuccess'
				// 	// })
				// });
			},
		}
	}
</script>

<style lang='scss'>
	.app {
		width: 100%;
	}

	.price-box {
		background-color: #fff;
		height: 265upx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #909399;

		.price {
			font-size: 50upx;
			color: #303133;
			margin-top: 12upx;

			&:before {
				content: '￥';
				font-size: 40upx;
			}
		}
	}

	.pay-type-list {
		margin-top: 20upx;
		background-color: #fff;
		padding-left: 60upx;

		.type-item {
			height: 120upx;
			padding: 20upx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-right: 60upx;
			font-size: 30upx;
			position: relative;
		}

		.icon {
			width: 100upx;
			font-size: 52upx;
		}

		.icon-erjiye-yucunkuan {
			color: #fe8e2e;
		}

		.icon-weixinzhifu {
			color: #36cb59;
		}

		.icon-alipay {
			color: #01aaef;
		}

		.tit {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 4upx;
		}

		.con {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-sm;
			color: $font-color-light;
		}
	}

	.mix-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 630upx;
		height: 80upx;
		margin: 80upx auto 30upx;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
